package com.example.bee_work_3.fragment.project;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.bee_work_3.R;

import java.util.List;

public class ProjectClassAdapter extends RecyclerView.Adapter<ProjectClassAdapter.ViewHolder> {
    private int mResource;

    private Context mContext;
    private List<String[]> mList;   //0:name   1:id
    private Fragment_Project.CallBack mCallBack;

    public ProjectClassAdapter(Context context, int resource, List<String[]> list, Fragment_Project.CallBack callBack) {
        this.mList = list;
        this.mResource = resource;
        this.mContext = context;
        this.mCallBack = callBack;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(mResource, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        final String[] item = mList.get(i);
        viewHolder.title.setText(item[0]);
        viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.onSelected(Integer.parseInt(item[1]));
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        CardView cardView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            cardView = itemView.findViewById(R.id.card);
        }
    }


}
